﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;

namespace YTSK.Report
{
    public class ReportData: ReportInit
    {
        public ReportData(Control main, DataGridView dg, string ReportCode) :
            base(main, dg, ReportCode)
        { }

        #region 计算查询条件
        public string GetControlValue()
        {
            var url = "";
            foreach (Control c in _main.Controls)
            {
                if (c.GetType() == typeof(DateTimePicker))
                {
                    url += string.Format("&{0}={1}", c.Name,((DateTimePicker)c).Value.ToString("yyyy-MM-dd"));
                }
                if (c.GetType() == typeof(ComboBox))
                {
                    var cb = (ComboBox)c;
                    var cval = cb.SelectedValue.ObjToString();
                    if(cb.DropDownStyle== ComboBoxStyle.DropDown)
                    {
                        cval = cb.Text.ObjToString();
                    }                    
                    if (cval == "全部") { cval = ""; }
                    url += string.Format("&{0}={1}", c.Name, cval);
                }
                if (c.GetType() == typeof(TextBox))
                {
                    url += string.Format("&{0}={1}", c.Name, ((TextBox)c).Text.Trim());
                }
            }
            url = url.Trim('&');
            return url;
        }
        #endregion

        #region 加载数据
        public virtual void GetGridData(object sender, EventArgs e)
        {
            GetGridData();
        }

        public virtual void GetGridData()
        {
            var f = new FrmTip();
            try
            {
                f.Show();
                f.setValue("正在获取数据，时间比较久，请耐心等候", 50);
                //var url = string.Format("{0}/api/oms/Report/ReportData?ReportCode={3}&token={1}&{2}",
                //        ServerConfig.ServerAddress, ServerConfig.Token, GetControlValue(), _reportcode);
                //var ss = Common.handleHttp.Get(url);
                //var result = Common.handleJSON.DeserializeResult(ss);
                //if (result.result != 1) { ServerConfig.Token = ""; MessageBox.Show("服务器错误:" + result.resultdetail); return; }
                //var data = result.GetListDic("data");
                var result = GetReportData(_reportcode, GetControlValue());
                if (result.result != 1) { return; }
                var data = result.GetListDic("data");
                f.setValue("格式化数据", 80);
                base._dg.Rows.Clear();
                var rows = result.GetVal("TotalRecords").ObjToInt();
                if (rows > data.Count) { rows = data.Count; }
                if (rows == 0)
                { //MessageBox.Show("没有查询到数据，请更改查询条件"); 
                    return;
                }
                _dg.Rows.Add(rows);

                for (int i = 0; i < rows; i++)
                {
                    var dic = data[i];
                    var dgr = _dg.Rows[i];

                    for (int j = 0; j < _dg.Columns.Count; j++)
                    {
                        var colname = _dg.Columns[j].Name;
                        if (dic.Keys.Contains(colname))
                        {
                            dgr.Cells[j].Value = dic[colname];
                        }
                    }
                    dgr.Cells["selcol"].Value = (i + 1).ToString();
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message + ex.StackTrace); }
            finally { f.Close(); }
        }
        #endregion

        #region 从服务器获取报表数据
        /// <summary>
        /// 从服务器获取报表数据
        /// </summary>
        /// <param name="reportcode">报表编号</param>
        /// <param name="otherval">其他条件</param>
        /// <returns></returns>
        public static Model.ResultJson GetReportData(string reportcode,string otherval="")
        {
            var url = string.Format("{0}/api/oms/Report/ReportData?ReportCode={3}&token={1}&{2}",
                        ServerConfig.ServerAddress, ServerConfig.GetToken(), otherval, reportcode);
            var ss = Common.handleHttp.Get(url);
            var result = Common.handleJSON.DeserializeResult(ss);
            if (result.result != 1) { MessageBox.Show("服务器错误:" + result.resultdetail); return new Model.ResultJson() ; }
            return result;
            //var data = result.GetListDic("data");
            //return data;
        }
        #endregion

    }
}
